<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Shell 脚本编写教程 | 前端一锅煮</title>
    <meta name="description" content="mac shell 脚本编写教程">
    <link rel="icon" href="/blog/logo.jpg">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="msapplication-TileColor" content="#000000">
    <meta name="keywords" content="前端一锅煮,mac,shell脚本编写教程,bash命令">
    <link rel="preload" href="/blog/assets/css/0.styles.3a36f1f3.css" as="style"><link rel="preload" href="/blog/assets/js/app.2d50d9dc.js" as="script"><link rel="preload" href="/blog/assets/js/2.55dfcea5.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.cf06b472.js"><link rel="prefetch" href="/blog/assets/js/11.57a175c6.js"><link rel="prefetch" href="/blog/assets/js/12.b32f2db2.js"><link rel="prefetch" href="/blog/assets/js/13.818a88b3.js"><link rel="prefetch" href="/blog/assets/js/14.1c54dd87.js"><link rel="prefetch" href="/blog/assets/js/15.40ea867f.js"><link rel="prefetch" href="/blog/assets/js/16.379721e3.js"><link rel="prefetch" href="/blog/assets/js/17.0b8bcd15.js"><link rel="prefetch" href="/blog/assets/js/18.41bb3889.js"><link rel="prefetch" href="/blog/assets/js/19.6362af40.js"><link rel="prefetch" href="/blog/assets/js/20.4ce40a8a.js"><link rel="prefetch" href="/blog/assets/js/21.ba4dcc65.js"><link rel="prefetch" href="/blog/assets/js/22.d2fad5dd.js"><link rel="prefetch" href="/blog/assets/js/23.2a976f6d.js"><link rel="prefetch" href="/blog/assets/js/24.a6407b77.js"><link rel="prefetch" href="/blog/assets/js/25.28818591.js"><link rel="prefetch" href="/blog/assets/js/26.a4d6759f.js"><link rel="prefetch" href="/blog/assets/js/27.fe72fdf4.js"><link rel="prefetch" href="/blog/assets/js/28.ca4a85a9.js"><link rel="prefetch" href="/blog/assets/js/29.4c6c0c28.js"><link rel="prefetch" href="/blog/assets/js/3.e235257b.js"><link rel="prefetch" href="/blog/assets/js/30.6aa9a972.js"><link rel="prefetch" href="/blog/assets/js/31.b7bccc14.js"><link rel="prefetch" href="/blog/assets/js/32.3544b6a6.js"><link rel="prefetch" href="/blog/assets/js/33.3bb697fd.js"><link rel="prefetch" href="/blog/assets/js/34.ac7cecf3.js"><link rel="prefetch" href="/blog/assets/js/35.b26fe96f.js"><link rel="prefetch" href="/blog/assets/js/36.91eb88f8.js"><link rel="prefetch" href="/blog/assets/js/37.baa3c248.js"><link rel="prefetch" href="/blog/assets/js/4.04108111.js"><link rel="prefetch" href="/blog/assets/js/5.2e3bc3e6.js"><link rel="prefetch" href="/blog/assets/js/6.88208dc4.js"><link rel="prefetch" href="/blog/assets/js/7.7913f494.js"><link rel="prefetch" href="/blog/assets/js/8.64ff3aa5.js"><link rel="prefetch" href="/blog/assets/js/9.43d70a25.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.3a36f1f3.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><!----> <span class="site-name">前端一锅煮</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="/blog/page/list/" class="nav-link router-link-active">全部文章</a></div><div class="nav-item"><a href="/blog/page/vuepress/" class="nav-link">VuePress笔记</a></div><div class="nav-item"><a href="/blog/page/about/" class="nav-link">关于我</a></div> <a href="https://gitee.com/cjm0/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
    Gitee
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="/blog/page/list/" class="nav-link router-link-active">全部文章</a></div><div class="nav-item"><a href="/blog/page/vuepress/" class="nav-link">VuePress笔记</a></div><div class="nav-item"><a href="/blog/page/about/" class="nav-link">关于我</a></div> <a href="https://gitee.com/cjm0/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
    Gitee
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>Shell 脚本编写教程</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/blog/page/list/sh.html#执行命令" class="sidebar-link">执行命令</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/page/list/sh.html#语法" class="sidebar-link">语法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/page/list/sh.html#基础语法" class="sidebar-link">基础语法</a></li><li class="sidebar-sub-header"><a href="/blog/page/list/sh.html#变量" class="sidebar-link">变量</a></li><li class="sidebar-sub-header"><a href="/blog/page/list/sh.html#表达式" class="sidebar-link">表达式</a></li><li class="sidebar-sub-header"><a href="/blog/page/list/sh.html#判断" class="sidebar-link">判断</a></li><li class="sidebar-sub-header"><a href="/blog/page/list/sh.html#条件语句" class="sidebar-link">条件语句</a></li><li class="sidebar-sub-header"><a href="/blog/page/list/sh.html#循环语句" class="sidebar-link">循环语句</a></li><li class="sidebar-sub-header"><a href="/blog/page/list/sh.html#函数" class="sidebar-link">函数</a></li></ul></li><li><a href="/blog/page/list/sh.html#传参" class="sidebar-link">传参</a><ul class="sidebar-sub-headers"></ul></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="shell-脚本编写教程"><a href="#shell-脚本编写教程" class="header-anchor">#</a> Shell 脚本编写教程</h1> <p>Shell 脚本与 Windows/Dos 下的批处理相似，也就是用各类命令预先放入到一个文件中，方便一次性执行的一个程序文件，主要是方便管理员进行设置或者管理用的。但是它比 Windows 下的批处理更强大，比用其他编程程序编辑的程序效率更高，它使用了 Linux/Unix 下的命令。</p> <p>更明白地来说，shell script 就像早期 dos 年代的 <a href="https://baike.baidu.com/item/%E6%89%B9%E5%A4%84%E7%90%86/1448600?fromtitle=.bat&fromid=6476412" target="_blank" rel="noopener noreferrer">.bat<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>，最简单的功能就是将许多指令汇整写一起，让使用者很容易地就能够一个操作执行多个命令，而 shell script 更是提供了数组，循环，条件以及逻辑判断等重要功能，让使用者可以直接以 shell 来写程序，而不必使用类似 C 程序语言等传统程序编写的语法。<a href="https://blog.csdn.net/qq_18297675/article/details/52693464" target="_blank" rel="noopener noreferrer">语法参考<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h2 id="执行命令"><a href="#执行命令" class="header-anchor">#</a> 执行命令</h2> <p>方法一： <code>bash test.sh</code></p> <p>方法二： 得先增加 test.sh 文件可执行权限</p> <p><code>chmod 755 test.sh</code></p> <p><code>./test.sh</code></p> <h2 id="语法"><a href="#语法" class="header-anchor">#</a> 语法</h2> <h3 id="基础语法"><a href="#基础语法" class="header-anchor">#</a> 基础语法</h3> <ol><li><p><code>#!/bin/bash</code> #! 是一个约定的标记，它告诉系统这个脚本需要什么解释器来执行，即使用哪一种 Shell</p></li> <li><p><code>set -e</code> 确保脚本抛出遇到的错误</p></li> <li><p>数据类型：只有字符串和整型两种</p></li> <li><p>使用双引号</p></li></ol> <h3 id="变量"><a href="#变量" class="header-anchor">#</a> 变量</h3> <p>shell 的变量赋值的时候不用带 <code>$</code>，而使用或者输出的时候要带 <code>$</code>。加减乘除的时候要加两层小括号。括号外面要有一个 <code>$</code>，括号里面的变量可以不用 <code>$</code>。需要注意的是，变量赋值，等号两边不能有空格，否则会被解析成命令，报错无此命令。</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 变量</span>
<span class="token assign-left variable">a</span><span class="token operator">=</span><span class="token number">10</span>
<span class="token assign-left variable">b</span><span class="token operator">=</span><span class="token number">10</span>
<span class="token assign-left variable">c</span><span class="token operator">=</span><span class="token string">&quot;this is test&quot;</span>
<span class="token assign-left variable">d</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$((</span>a<span class="token operator">+</span>b<span class="token variable">))</span></span>

<span class="token builtin class-name">echo</span> <span class="token variable">$a</span> 
<span class="token builtin class-name">echo</span> <span class="token variable">$c</span>
<span class="token builtin class-name">echo</span> <span class="token variable">$d</span>
<span class="token builtin class-name">echo</span> <span class="token variable"><span class="token variable">$((</span>a<span class="token operator">+</span>b<span class="token variable">))</span></span>
</code></pre></div><p><img src="" alt="输出"></p> <h3 id="表达式"><a href="#表达式" class="header-anchor">#</a> 表达式</h3> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token assign-left variable">str</span><span class="token operator">=</span><span class="token string">&quot;a b c d e f g h i j&quot;</span>

<span class="token builtin class-name">echo</span> <span class="token variable">${str}</span>          <span class="token comment">#源字符串</span>
<span class="token builtin class-name">echo</span> <span class="token variable">${<span class="token operator">#</span>str}</span>         <span class="token comment">#字符串长度，包含空格</span>
<span class="token builtin class-name">echo</span> <span class="token variable">${str<span class="token operator">:</span>5}</span>        <span class="token comment">#截取从第五个后面开始到最后的字符</span>
<span class="token builtin class-name">echo</span> <span class="token variable">${str<span class="token operator">:</span>5<span class="token operator">:</span>2}</span>      <span class="token comment">#截取从第五个后面开始的2个字符</span>
<span class="token builtin class-name">echo</span> <span class="token variable">${str<span class="token operator">#</span>a*f}</span>      <span class="token comment">#从开头删除a到f的字符</span>
<span class="token builtin class-name">echo</span> <span class="token variable">${str<span class="token operator">##</span>a*}</span>      <span class="token comment">#从开头删除a以后的字符</span>
<span class="token builtin class-name">echo</span> <span class="token variable">${str<span class="token operator">%</span>f*j}</span>      <span class="token comment">#从结尾删除f到j的字符</span>
<span class="token builtin class-name">echo</span> <span class="token variable">${str<span class="token operator">%%</span>*j}</span>      <span class="token comment">#从结尾删除j前面的所有字符包括j</span>
<span class="token builtin class-name">echo</span> <span class="token variable">${str}</span>
</code></pre></div><p><img src="/blog/assets/img/sh2.943f3c18.jpg" alt="输出"></p> <h3 id="判断"><a href="#判断" class="header-anchor">#</a> 判断</h3> <p>[ ] 里面每个变量之间都要有空格</p> <p><code>-gt &gt;</code> <code>-ge &gt;=</code> <code>-lt &lt;</code> <code>-le &lt;=</code> <code>-eq =</code> <code>-ne !=</code></p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 判断 test 或 []</span>
<span class="token assign-left variable">num1</span><span class="token operator">=</span><span class="token number">10</span>
<span class="token assign-left variable">num2</span><span class="token operator">=</span><span class="token number">10</span>

<span class="token builtin class-name">test</span> <span class="token variable">$num1</span> -eq <span class="token variable">$num2</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">echo</span> <span class="token string">&quot;num1 == num2&quot;</span> <span class="token operator">||</span> <span class="token builtin class-name">echo</span> <span class="token string">&quot;num1 != num2&quot;</span>

<span class="token punctuation">[</span> <span class="token variable">$num1</span> -eq <span class="token variable">$num2</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">echo</span> <span class="token string">&quot;num1 == num2&quot;</span> <span class="token operator">||</span> <span class="token builtin class-name">echo</span> <span class="token string">&quot;num1 != num2&quot;</span>

</code></pre></div><p><img src="" alt="输出"></p> <h3 id="条件语句"><a href="#条件语句" class="header-anchor">#</a> 条件语句</h3> <p>格式：if 条件 ; then 结果 fi ，最后面一定要有 fi 。在 shell 脚本里面，控制分支结构结束都要和开头的单词相反，例如，<code>if &lt;–&gt; fi</code>，<code>case &lt;–&gt; esac</code>。</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token assign-left variable">grades</span><span class="token operator">=</span><span class="token number">70</span>

<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$grades</span> -ge <span class="token number">90</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">[</span> <span class="token variable">$grades</span> -le <span class="token number">100</span> <span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">then</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;Your grade is excellent.&quot;</span>
<span class="token keyword">elif</span> <span class="token punctuation">[</span> <span class="token variable">$grades</span> -ge <span class="token number">80</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">[</span> <span class="token variable">$grades</span> -le <span class="token number">89</span> <span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">then</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;Your grade is good.&quot;</span>
<span class="token keyword">elif</span> <span class="token punctuation">[</span> <span class="token variable">$grades</span> -ge <span class="token number">70</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">[</span> <span class="token variable">$grades</span> -le <span class="token number">79</span> <span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">then</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;Your grade is middle.&quot;</span>
<span class="token keyword">elif</span> <span class="token punctuation">[</span> <span class="token variable">$grades</span> -ge <span class="token number">60</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">[</span> <span class="token variable">$grades</span> -le <span class="token number">69</span> <span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">then</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;Your grade is passing.&quot;</span>
<span class="token keyword">else</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;Your grade is badly.&quot;</span>
<span class="token keyword">fi</span>
</code></pre></div><h3 id="循环语句"><a href="#循环语句" class="header-anchor">#</a> 循环语句</h3> <h4 id="while-语句"><a href="#while-语句" class="header-anchor">#</a> while 语句</h4> <p>while 语句是只要条件为真就执行下面语句。需要注意的是，这里的条件除了 while true 可以这样写，其它的条件都要用 test 或者 [ ] 来判断</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token assign-left variable">i</span><span class="token operator">=</span><span class="token variable">$1</span>
<span class="token keyword">while</span> <span class="token punctuation">[</span> <span class="token variable">$i</span> -gt <span class="token number">0</span> <span class="token punctuation">]</span>
<span class="token keyword">do</span>
<span class="token builtin class-name">echo</span> <span class="token variable">$i</span>
<span class="token variable"><span class="token punctuation">((</span>i<span class="token operator">--</span><span class="token punctuation">))</span></span>
<span class="token keyword">done</span>
</code></pre></div><p><img src="" alt="输出"></p> <h4 id="until-语句"><a href="#until-语句" class="header-anchor">#</a> until 语句</h4> <p>until 语句是只要条件为假就执行下列语句。</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token assign-left variable">i</span><span class="token operator">=</span><span class="token variable">$1</span>
<span class="token keyword">until</span> <span class="token punctuation">[</span> <span class="token variable">$i</span> -le <span class="token number">0</span> <span class="token punctuation">]</span>
<span class="token keyword">do</span>
<span class="token builtin class-name">echo</span> <span class="token variable">$i</span>
<span class="token variable"><span class="token punctuation">((</span>i<span class="token operator">--</span><span class="token punctuation">))</span></span>
<span class="token keyword">done</span>
</code></pre></div><p><img src="" alt="输出"></p> <h4 id="for-语句"><a href="#for-语句" class="header-anchor">#</a> for 语句</h4> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token assign-left variable">a</span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">seq</span> <span class="token number">4</span> <span class="token number">10</span><span class="token variable">`</span></span>

<span class="token builtin class-name">echo</span> <span class="token variable">$a</span>

<span class="token keyword">for</span> <span class="token for-or-select variable">i</span> <span class="token keyword">in</span> <span class="token variable"><span class="token variable">`</span><span class="token function">seq</span> <span class="token number">4</span> <span class="token number">10</span><span class="token variable">`</span></span> <span class="token comment">#seq是一个命令，顺序生成一串数字或者字符</span>
<span class="token keyword">do</span>
   <span class="token builtin class-name">echo</span> <span class="token variable">$i</span>
<span class="token keyword">done</span>
</code></pre></div><p><img src="" alt="输出"></p> <h3 id="函数"><a href="#函数" class="header-anchor">#</a> 函数</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">funcName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
    语句 
    <span class="token punctuation">[</span><span class="token keyword">return</span> 返回值<span class="token punctuation">]</span> 
<span class="token punctuation">}</span> 
</code></pre></div><p>返回值是可选的，如果没有显示 return 则默认返回最后一条语句执行的结果。</p> <p>Shell 函数返回值只能是整数，一般用来表示函数执行成功与否，0表示成功，其他值表示失败。如果 return 其他数据，比如一个字符串，往往会得到错误提示：<code>numeric argument required</code>。</p> <p>如果一定要让函数返回字符串，那么可以先定义一个变量，用来接收函数的计算结果，脚本在需要的时候访问这个变量来获得函数返回值。</p> <p>函数参数从 <code>$1</code> 到 <code>$n</code>，<code>$0</code> 是文件名。</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 打印数字</span>
<span class="token function-name function">printNum</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
   <span class="token builtin class-name">echo</span> <span class="token variable">$1</span>
<span class="token punctuation">}</span>

<span class="token keyword">for</span> <span class="token for-or-select variable">i</span> <span class="token keyword">in</span> <span class="token variable"><span class="token variable">`</span><span class="token function">seq</span> <span class="token number">2</span> <span class="token number">8</span><span class="token variable">`</span></span> <span class="token comment">#seq是一个命令，顺序生成一串数字或者字符</span>
<span class="token keyword">do</span>
printNum <span class="token variable">$i</span>
<span class="token keyword">done</span>
</code></pre></div><h2 id="传参"><a href="#传参" class="header-anchor">#</a> 传参</h2> <p>采用 <code>$0</code> , <code>$1</code> , <code>$2</code> ...等方式获取脚本命令行传入的参数，<code>$0</code> 获取到的是脚本路径以及脚本名，后面按顺序获取参数，当参数超过10个时(包括10个)，需要使用 <code>${10}</code>, <code>${11}</code> ...才能获取到参数，但是一般很少会超过10个参数的情况。</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token shebang important">#!/bin/bash</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;脚本名<span class="token variable">$0</span>&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第一个参数<span class="token variable">$1</span>&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第二个参数<span class="token variable">$2</span>&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第三个参数<span class="token variable">$3</span>&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第四个参数<span class="token variable">$4</span>&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第五个参数<span class="token variable">$5</span>&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第六个参数<span class="token variable">$6</span>&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第七个参数<span class="token variable">$7</span>&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第八个参数<span class="token variable">$8</span>&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第九个参数<span class="token variable">$9</span>&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第十个参数<span class="token variable">$10</span>&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;第十个参数<span class="token variable">${10}</span>&quot;</span>
</code></pre></div><p><img src="/blog/assets/img/sh4.f1dec063.jpg" alt="输出"></p></div> <div class="page-edit"><div class="edit-link"><a href="https://gitee.com/cjm0/blog/edit/master/docs/page/list/sh.md" target="_blank" rel="noopener noreferrer">在 Gitee 上编辑此页</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <!----></div> <!----> </div> <!----></div></div>
    <script src="/blog/assets/js/app.2d50d9dc.js" defer></script><script src="/blog/assets/js/2.55dfcea5.js" defer></script>
  </body>
</html>
